package com.dog.basics.service.mtkj.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.mtkj.FlowingWaterFromDto;
import com.dog.basics.bean.model.common.DogUser;
import com.dog.basics.bean.model.mtkj.MtDeviceFlowingWater;
import com.dog.basics.bean.model.mtkj.MtDeviceOrder;
import com.dog.basics.bean.vo.admin.mtkj.FlowingWaterVo;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.common.DogUserMapper;
import com.dog.basics.dao.mtkj.MtDeviceFlowingWaterMapper;
import com.dog.basics.dao.mtkj.MtDeviceOrderMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.mtkj.IMtDeviceFlowingWaterService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备流水 服务实现类
 * </p>
 *
 * @author wph
 * @since 2020-09-29
 */
@Service
@AllArgsConstructor
public class MtDeviceFlowingWaterServiceImpl extends ServiceImpl<MtDeviceFlowingWaterMapper, MtDeviceFlowingWater> implements IMtDeviceFlowingWaterService {
    private final DogUserMapper dogUserMapper;
    private final MtDeviceOrderMapper mtDeviceOrderMapper;

    /**
     * 分页的条件公用类
     *
     * @param name,mobile
     * @param userIds
     */
    public List<Long> getuserIdsWhere(String name, String mobile, List<Long> userIds) {
        List<Long> collect = dogUserMapper.selectList(Wrappers.<DogUser>lambdaQuery().like(StrUtil.isNotBlank(mobile), DogUser::getUserMobile, mobile).like(StrUtil.isNotBlank(name), DogUser::getNickName, name)).stream().map(e -> e.getUserId()).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(collect)) {
            userIds = collect;
        } else {
            userIds.add(-2L);
        }
        return userIds;
    }

    /**
     * @Description: 分页设备流水
     * @Param: status
     * @return: IPage<FlowingWaterVo>
     * @Author: wph
     * @Date:
     */
    @Override
    public IPage<FlowingWaterVo> getPageList(PageParam page, FlowingWaterFromDto obj) {
        List<String> serialNos = CollUtil.newArrayList();
        //权限判断
        if (SecurityUtils.getSysUser().getAccountType() == 2) {
            //查询属于自己的订单
            serialNos = mtDeviceOrderMapper.selectList(Wrappers.<MtDeviceOrder>lambdaQuery().eq(MtDeviceOrder::getOrderAgentUserId, SecurityUtils.getSysUser().getUserId())).stream().map(e -> e.getSerialNo()).collect(Collectors.toList());
            if (CollUtil.isEmpty(serialNos)) {
                serialNos.add("-2L");
            }

        }
        // 判断所属用户ID
        List<Long> userIds = CollUtil.newArrayList();
        if (StrUtil.isNotBlank(obj.getUserName()) || StrUtil.isNotBlank(obj.getUserMobile())) {
            userIds = getuserIdsWhere(obj.getUserName(), obj.getUserMobile(), userIds);
        }
        IPage iPage = this.page(page, new LambdaQueryWrapper<MtDeviceFlowingWater>()
                .in(CollUtil.isNotEmpty(serialNos), MtDeviceFlowingWater::getSerialNo, serialNos)
                .in(CollUtil.isNotEmpty(userIds), MtDeviceFlowingWater::getWaterUserId, userIds)
                .like(StrUtil.isNotBlank(obj.getSerialNo()), MtDeviceFlowingWater::getSerialNo, obj.getSerialNo())
                .eq(ObjectUtil.isNotNull(obj.getWaterAccount()), MtDeviceFlowingWater::getWaterAccount, obj.getWaterAccount())
                .eq(ObjectUtil.isNotNull(obj.getInoutType()), MtDeviceFlowingWater::getInoutType, obj.getInoutType())
                .eq(ObjectUtil.isNotNull(obj.getWaterType()), MtDeviceFlowingWater::getWaterType, obj.getWaterType())
                .between(ObjectUtil.isNotNull(obj.getStartTime()) && ObjectUtil.isNotNull(obj.getEndTime()), MtDeviceFlowingWater::getCreateTime, obj.getStartTime(), obj.getEndTime())
                .orderBy(Boolean.TRUE, obj.getIsOrderByAvg(), MtDeviceFlowingWater::getCreateTime));
        List<MtDeviceFlowingWater> records = iPage.getRecords();
        iPage.setRecords(records.stream().map(e -> {
            FlowingWaterVo newsVo = new FlowingWaterVo();
            BeanUtil.copyProperties(e, newsVo);
            //查询
            DogUser dogUser = dogUserMapper.selectById(e.getWaterUserId());
            newsVo.setUserName(ObjectUtil.isNotNull(dogUser) ? dogUser.getNickName() : "");
            newsVo.setUserMobile(ObjectUtil.isNotNull(dogUser) ? dogUser.getUserMobile() : "");
            return newsVo;
        }).collect(Collectors.toList()));
        return iPage;
    }


    /**
     * @Description: 删除设备流水
     * @Param: ids
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteObj(Long id) {
        MtDeviceFlowingWater dogNews = this.getById(id);
        Validator.validateNotNull(dogNews, "无此数据!");
        Validator.validateTrue(this.removeById(id), "数据删除失败!");
        return Boolean.TRUE;
    }
}
